package com.lv.suyiyong.hfrbusines.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.Guideline
import com.lv.suyiyong.hfrbusines.R
import com.lv.suyiyong.hfrbusines.databinding.LayoutHomeBottomTabBinding


class HomeBottomTabWidget @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : BaseHomeBottomTabWidget(context, attrs, defStyleAttr),
    View.OnClickListener {

    private var textViews: ArrayList<TextView>? = null

    //    private var mIvHomeTab: ImageView? = null//声明成可空类型
    private var mTotalUnread: TextView? = null//声明成可空类型
    private var mContactsView: TextView? = null//声明成可空类型
    private var mClientView: TextView? = null//声明成可空类型
    private var mGuidLine: Guideline? = null//声明成可空类型
    private var mGuidLine1: Guideline? = null//声明成可空类型

    /**
     * 初始化 设置点击事件。
     *
     */
    init { //默认第一个碎片
        val view = LayoutHomeBottomTabBinding.inflate(LayoutInflater.from(context), this, true)
        view.apply {
//            mIvHomeTab = ivHomeTab
            mTotalUnread = msgTotalUnread
            mGuidLine = guideline
            mGuidLine1 = guideline1
//            mClientView = llClientATHome
//            mContactsView = llHomeATContacts
            textViews = arrayListOf(
                llHomeATHome,
//                llClientATHome,
//                llHomeATMessage,
//                llHomeATContacts,
                llHomeATMine
            )
        }
        for (textView in textViews!!) {
            textView.setOnClickListener(this)
        }
    }

    /**
     * 销毁，避免内存泄漏
     */
    override fun destroy() {
        super.destroy()
        if (!textViews.isNullOrEmpty()) {
            textViews?.clear()
            textViews = null
        }
    }

    /**
     * 实现按钮的点击事件
     */
    override fun onClick(v: View) {
        when (v.id) {
            R.id.llHomeATHome -> fragmentManger(0)
//            R.id.llClientATHome -> fragmentManger(1)
//            R.id.llHomeATMessage -> fragmentManger(2)
//            R.id.llHomeATContacts -> fragmentManger(3)
            R.id.llHomeATMine -> fragmentManger(1)
        }
    }

    /**
     * fragment的切换 实现底部导航栏的切换
     *
     * @param position 序号
     */
    override fun fragmentManger(position: Int) {
        super.fragmentManger(position)
        for (j in textViews!!.indices) {
            textViews!![j].isSelected = position == j
        }
        listener?.OnClickTab(position)
    }

    fun setTabGone(tab: Int) {
        when (tab) {
            1 -> {
                mClientView?.visibility = View.GONE
                mGuidLine?.setGuidelinePercent(0.25f)
                mGuidLine1?.setGuidelinePercent(0.5f)
            }
            3 -> {
                mContactsView?.visibility = View.GONE
                mGuidLine?.setGuidelinePercent(0.5f)
                mGuidLine1?.setGuidelinePercent(0.75f)
            }
            13 -> {
                mClientView?.visibility = View.GONE
                mContactsView?.visibility = View.GONE
                mGuidLine?.setGuidelinePercent(0.33f)
                mGuidLine1?.setGuidelinePercent(0.66f)
            }

        }
    }

    fun setMessageCount(count: String) {
        if (count == "0") {
            mTotalUnread?.visibility = GONE
        } else {
            mTotalUnread?.visibility = VISIBLE
        }
        mTotalUnread?.text = count
    }

    interface TabPagerListener {
        fun OnClickTab(position: Int)
    }

    fun setOnTabPagerListener(l: TabPagerListener) {
        listener = l
    }


    var listener: TabPagerListener? = null

}